﻿using HomagChina.Basic.CCT.PLC;
using HomagGroup.Base.Communication.Items;
using System;
using System.Linq;
using System.Threading;

namespace Proxy
{

    public class OutfeedTransportHandle
    {
        ModifyInfoHandle Handle_Modify;
        PlcHandle Handle_Plc;
        public OutfeedTransportHandle()
        {
            Handle_Modify = new ModifyInfoHandle();
            Handle_Plc = new PlcHandle();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Location"></param>
        public void Position_Transport(string Location)
        {
            Item Item_OrderState = null;
            Item Item_Reference = null;
            var log = SerilogHandle.Logger_CT020;
            do
            {
            Start:
                Thread.Sleep(1000);
                try
                {
                    if (Globle.isOutfeedAreaActive)
                    {
                        switch (Location)
                        {
                            case "CT020":
                                Item_OrderState = Wholesale.Item_AOF_CT020_OrderState;
                                Item_Reference = Wholesale.Item_AOF_CT020_Reference;
                                log = SerilogHandle.Logger_CT020;
                                break;
                            default:
                                break;
                        }

                        var orderState = (int)(Handle_Plc.Read(Item_OrderState, "Int"));
                        var Reference = Handle_Plc.Read(Item_Reference, "String").ToString();
                        if ((!orderState.Equals((int)EnumOfOrderState.Plc请求数据)
                            && !orderState.Equals((int)EnumOfOrderState.Plc完成任务))
                            || string.IsNullOrWhiteSpace(Reference))
                        {
                            goto Start;
                        }

                        log.Information("Reference :" + Reference);
                        log.Information("orderState :" + orderState);
                        switch (orderState)
                        {
                            case (int)EnumOfOrderState.Plc完成任务:
                            Write2Plc:
                                ResultCode WriteFlag = Handle_Plc.Write(Item_OrderState, (int)EnumOfOrderState.CCT完成任务);
                                if (WriteFlag != ResultCode.Success)
                                {
                                    goto Write2Plc;
                                }
                                Handle_Modify.ModifyTransState(Reference);
                                break;
                            case (int)EnumOfOrderState.Plc请求数据:
                                Handle_Plc.Write(Item_OrderState, (int)EnumOfOrderState.CCT下发任务);
                                break;
                            default:
                                break;
                        }
                        goto Start;
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                    goto Start;
                }
            } while (true);
        }
    }
}
